home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / ntunzip2.zip / ZIPCLOAK.C < prev    next >
C/C++ Source or Header  |  1993-09-02  |  10KB  |  320 lines

  1. /*
  2.    This code is not copyrighted and is put in the public domain. It
  3.    was originally written in Europe and can be freely distributed from
  4.    any country except the U.S.A. If this code is imported in the U.S.A,
  5.    it cannot be re-exported from the U.S.A to another country. (This
  6.    restriction might seem curious but this is what US law requires.)
  7.  */
  8.  
  9. #define UTIL
  10. #include "revision.h"
  11. #include "zip.h"
  12. #include "crypt.h"
  13. #include <signal.h>
  14. #ifdef MSDOS
  15. #  include <stdlib.h>
  16. #endif
  17.  
  18.  
  19. /* Local functions */
  20. void err OF((int code, char *msg));
  21.  
  22. #ifdef CRYPT   /* defined (or not) in crypt.h */
  23.  
  24. local void handler OF((int sig));
  25. local void license OF((void));
  26. local void help OF((void));
  27. void main OF((int argc, char **argv));
  28.  
  29. /* Temporary zip file name and file pointer */
  30. local char *tempzip;
  31. local FILE *tempzf;
  32.  
  33. #endif
  34.  
  35. /***********************************************************************
  36.  * Issue a message for the error, clean up files and memory, and exit.
  37.  */
  38. void err(code, msg)
  39.     int code;               /* error code from the ZE_ class */
  40.     char *msg;              /* message about how it happened */
  41. {
  42. #ifdef CRYPT
  43.     if (PERR(code)) perror("zipcloak error");
  44.     fprintf(stderr, "zipcloak error: %s (%s)\n", errors[code-1], msg);
  45.     if (tempzf != NULL) fclose(tempzf);
  46.     if (tempzip != NULL) {
  47.         destroy(tempzip);
  48.         free((voidp *)tempzip);
  49.     }
  50.     if (zipfile != NULL) free((voidp *)zipfile);
  51. #endif
  52. #ifdef VMS
  53.     exit(0);
  54. #else
  55.     exit(code);
  56. #endif
  57. }
  58.  
  59. /***********************************************************************
  60.  * Print a warning message to stderr and return.
  61.  */
  62. void warn(msg1, msg2)
  63.     char *msg1, *msg2;        /* message strings juxtaposed in output */
  64. {
  65.     fprintf(stderr, "zipcloak warning: %s%s\n", msg1, msg2);
  66. }
  67.  
  68. #ifdef CRYPT
  69.  
  70. /***********************************************************************
  71.  * Upon getting a user interrupt, turn echo back on for tty and abort
  72.  * cleanly using err().
  73.  */
  74. local void handler(sig)
  75.     int sig;                  /* signal number (ignored) */
  76. {
  77. #if !defined(MSDOS) && !defined(__human68k__)
  78.     echon();
  79.     putc('\n', stderr);
  80. #endif
  81.     err(ZE_ABORT +sig-sig, "aborting");
  82.     /* dummy usage of sig to avoid compiler warnings */
  83. }
  84.  
  85.  
  86. static char *public[] = {
  87. "The encryption code of this program is not copyrighted and is put in the",
  88. "public domain. It was originally written in Europe and can be freely",
  89. "distributed from any country except the U.S.A. If this program is imported",
  90. "in the U.S.A, it cannot be re-exported from the U.S.A to another country.",
  91. "The copyright notice of the zip program applies to the rest of the code."
  92. };
  93.  
  94. /***********************************************************************
  95.  * Print license information to stdout.
  96.  */
  97. local void license()
  98. {
  99.     extent i;             /* counter for copyright array */
  100.  
  101.     for (i = 0; i < sizeof(public)/sizeof(char *); i++) {
  102.         puts(public[i]);
  103.     }
  104.     for (i = 0; i < sizeof(disclaimer)/sizeof(char *); i++) {
  105.         puts(disclaimer[i]);
  106.     }
  107. }
  108.  
  109.  
  110. static char *help_info[] = {
  111. "",
  112. "ZipCloak %s (%s)",
  113. "Usage:  zipcloak [-d] [-b path] zipfile",
  114. "  the default action is to encrypt all unencrypted entries in the zip file",
  115. "  -d   decrypt--decrypt encrypted entries (copy if given wrong password)",
  116. "  -b   use \"path\" for the temporary zip file",
  117. "  -h   show this help               -L   show software license"
  118.   };
  119.  
  120. /***********************************************************************
  121.  * Print help (along with license info) to stdout.
  122.  */
  123. local void help()
  124. {
  125.     extent i;             /* counter for help array */
  126.  
  127.     for (i = 0; i < sizeof(public)/sizeof(char *); i++) {
  128.         puts(public[i]);
  129.     }
  130.     for (i = 0; i < sizeof(help_info)/sizeof(char *); i++) {
  131.         printf(help_info[i], VERSION, REVDATE);
  132.         putchar('\n');
  133.     }
  134. }
  135.  
  136.  
  137. /***********************************************************************
  138.  * Encrypt or decrypt all of the entries in a zip file.  See the command
  139.  * help in help() above.
  140.  */
  141.  
  142. void main(argc, argv)
  143.     int argc;             /* number of tokens in command line */
  144.     char **argv;          /* command line tokens */
  145. {
  146.     int attr;             /* attributes of zip file */
  147.     ulg start_offset;     /* start of central directory */
  148.     int decrypt;          /* decryption flag */
  149.     int temp_path;        /* 1 if next argument is path for temp files */
  150.     char passwd[PWLEN+1]; /* password for encryption or decryption */
  151.     char *q;              /* steps through option arguments */
  152.     int r;                /* arg counter */
  153.     int res;              /* result code */
  154.     ulg length;           /* length of central directory */
  155.     FILE *inzip, *outzip; /* input and output zip files */
  156.     struct zlist far *z;  /* steps through zfiles linked list */
  157.  
  158.  
  159.     /* If no args, show help */
  160.     if (argc == 1) {
  161.         help();
  162.         exit(0);
  163.     }
  164.  
  165.     init_upper();           /* build case map table */
  166.  
  167.     /* Go through args */
  168.     zipfile = tempzip = NULL;
  169.     tempzf = NULL;
  170.     signal(SIGINT, handler);
  171. #ifdef SIGTERM                /* Some don't have SIGTERM */
  172.     signal(SIGTERM, handler);
  173. #endif
  174.     temp_path = decrypt = 0;
  175.     for (r = 1; r < argc; r++) {
  176.         if (*argv[r] == '-') {
  177.             if (!argv[r][1]) err(ZE_PARMS, "zip file cannot be stdin");
  178.             for (q = argv[r]+1; *q; q++) {
  179.                 switch(*q) {
  180.                 case 'b':   /* Specify path for temporary file */
  181.                     if (temp_path) {
  182.                         err(ZE_PARMS, "use -b before zip file name");
  183.                     }
  184.                     temp_path = 1;          /* Next non-option is path */
  185.                     break;
  186.                 case 'd':
  187.                     decrypt = 1;  break;
  188.                 case 'h':   /* Show help */
  189.                     help();
  190.                     exit(0);
  191.                 case 'l': case 'L':  /* Show copyright and disclaimer */
  192.                     license();
  193.                     exit(0);
  194.                 default:
  195.                     err(ZE_PARMS, "unknown option");
  196.                 } /* switch */
  197.             } /* for */
  198.  
  199.         } else if (temp_path == 0) {
  200.             if (zipfile != NULL) {
  201.                 err(ZE_PARMS, "can only specify one zip file");
  202.  
  203.             } else if ((zipfile = ziptyp(argv[r])) == NULL) {
  204.                 err(ZE_MEM, "was processing arguments");
  205.             }
  206.         } else {
  207.             tempath = argv[r];
  208.             temp_path = 0;
  209.         } /* if */
  210.     } /* for */
  211.  
  212.     if (zipfile == NULL) err(ZE_PARMS, "need to specify zip file");
  213.  
  214.     /* Read zip file */
  215.     if ((res = readzipfile()) != ZE_OK) err(res, zipfile);
  216.     if (zfiles == NULL) err(ZE_NAME, zipfile);
  217.  
  218.     /* Check for something to do */
  219.     for (z = zfiles; z != NULL; z = z->nxt) {
  220.         if (decrypt ? z->flg & 1 : !(z->flg & 1)) break;
  221.     }
  222.     if (z == NULL) {
  223.         err(ZE_NONE, decrypt ? "no encrypted files"
  224.                        : "all files encrypted already");
  225.     }
  226.  
  227.     /* Before we get carried away, make sure zip file is writeable */
  228.     if ((inzip = fopen(zipfile, "a")) == NULL) err(ZE_CREAT, zipfile);
  229.     fclose(inzip);
  230.     attr = getfileattr(zipfile);
  231.  
  232.     /* Open output zip file for writing */
  233.     if ((tempzf = outzip = fopen(tempzip = tempname(zipfile), FOPW)) == NULL) {
  234.         err(ZE_TEMP, tempzip);
  235.     }
  236.  
  237.     /* Get password */
  238.     if (getp("Enter password: ", passwd, PWLEN+1) == NULL) {
  239.         err(ZE_PARMS, "stderr is not a tty (you may never see this message!)");
  240.     }
  241.  
  242.     /* Open input zip file again, copy preamble if any */
  243.     if ((inzip = fopen(zipfile, FOPR)) == NULL) err(ZE_NAME, zipfile);
  244.  
  245.     if (zipbeg && (res = fcopy(inzip, outzip, zipbeg)) != ZE_OK) {
  246.         err(res, res == ZE_TEMP ? tempzip : zipfile);
  247.     }
  248.     /* Go through local entries, copying, encrypting, or decrypting */
  249.     for (z = zfiles; z != NULL; z = z->nxt) {
  250.         if (decrypt && (z->flg & 1)) {
  251.             printf("decrypting: %s", z->zname);
  252.             fflush(stdout);
  253.             if ((res = zipbare(z, inzip, outzip, passwd)) != ZE_OK) {
  254.                 if (res != ZE_MISS) err(res, "was decrypting an entry");
  255.                 printf(" (wrong password--just copying)");
  256.             }
  257.             putchar('\n');
  258.  
  259.         } else if ((!decrypt) && !(z->flg & 1)) {
  260.             printf("encrypting: %s\n", z->zname);
  261.             fflush(stdout);
  262.             if ((res = zipcloak(z, inzip, outzip, passwd)) != ZE_OK) {
  263.                 err(res, "was encrypting an entry");
  264.             }
  265.         } else {
  266.             printf("   copying: %s\n", z->zname);
  267.             fflush(stdout);
  268.             if ((res = zipcopy(z, inzip, outzip)) != ZE_OK) {
  269.                 err(res, "was copying an entry");
  270.             }
  271.         } /* if */
  272.     } /* for */
  273.     fclose(inzip);
  274.  
  275.     /* Write central directory and end of central directory */
  276.  
  277.     /* get start of central */
  278.     if ((start_offset = ftell(outzip)) == -1L) err(ZE_TEMP, tempzip);
  279.  
  280.     for (z = zfiles; z != NULL; z = z->nxt) {
  281.         if ((res = putcentral(z, outzip)) != ZE_OK) err(res, tempzip);
  282.     }
  283.  
  284.     /* get end of central */
  285.     if ((length = ftell(outzip)) == -1L) err(ZE_TEMP, tempzip);
  286.  
  287.     length -= start_offset;               /* compute length of central */
  288.     if ((res = putend((int)zcount, length, start_offset, zcomlen,
  289.                        zcomment, outzip)) != ZE_OK) {
  290.         err(res, tempzip);
  291.     }
  292.     tempzf = NULL;
  293.     if (fclose(outzip)) err(ZE_TEMP, tempzip);
  294.     if ((res = replace(zipfile, tempzip)) != ZE_OK) {
  295.         warn("new zip file left as: ", tempzip);
  296.         free((voidp *)tempzip);
  297.         tempzip = NULL;
  298.         err(res, "was replacing the original zip file");
  299.     }
  300.     free((voidp *)tempzip);
  301.     tempzip = NULL;
  302.     setfileattr(zipfile, attr);
  303.     free((voidp *)zipfile);
  304.     zipfile = NULL;
  305.  
  306.     /* Done! */
  307.     exit(0);
  308. }
  309. #else /* !CRYPT */
  310.  
  311. void main()
  312. {
  313.     fprintf(stderr, "\
  314. This version of ZipCloak does not support encryption.  Get zcrypt20.zip (or\n\
  315. a later version) and recompile.  The Info-ZIP file `Where' lists sites.\n");
  316.     exit(1);
  317. }
  318.  
  319. #endif /* ?CRYPT */
  320.